package fun.codefarmer.pm.service;

import com.alibaba.druid.util.StringUtils;
import fun.codefarmer.pm.dto.HrDto;
import fun.codefarmer.pm.mapper.HrMapper;
import fun.codefarmer.pm.mapper.HrRoleMapper;
import fun.codefarmer.pm.model.Hr;
import fun.codefarmer.pm.model.HrRole;
import fun.codefarmer.pm.model.RespBean;
import fun.codefarmer.pm.model.Role;
import fun.codefarmer.pm.utils.ConvertUtils;
import fun.codefarmer.pm.utils.HrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ ClassName HrService
 * @ Descriotion TODO
 * @ author  codeFarmer_z
 * @ 公众号：码农教程
 * @ Date 2022/3/19 20:41
 **/
@Service
public class HrService implements UserDetailsService {
    @Resource
    private HrMapper hrMapper;
    @Autowired
    private MenuService menuService;
    @Resource
    private HrRoleMapper hrRoleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名 查找是否存在这个人
        Hr hr = hrMapper.loadUserByUsername(username);
        if (hr == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<Role> roleList = menuService.getHrRolesById(hr.getId());
        hr.setRoles(roleList);
        return hr;
    }

    public List<Hr> getAll(String name) {
        //自己id,除去自己以外的。
        Integer id = HrUtils.getCurrentHr().getId();
        List<Hr> allHr = hrMapper.getAllHr(id, name);
        return allHr;
    }

    public RespBean deleteHrRole(Integer id) {
        int i = hrMapper.deleteByPrimaryKey(id);

        hrRoleMapper.deleteByHrId(id);
        if (i == 1) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");

    }

    public RespBean updateHr(Hr hr) {
       int i = hrMapper.updateByPrimaryKeySelective(hr);
        if (i == 1) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }

    public HrDto getHrById(Hr hr) {
        Hr one = hrMapper.selectByPrimaryKey(hr.getId());
        // 查询这个用户所有角色
        List<Integer> hrRoleList = hrRoleMapper.getRoleBhrId(hr.getId());
        HrDto hrDto = ConvertUtils.sourceToTarget(one, HrDto.class);
        hrDto.setRoleIds(hrRoleList);
        hrDto.setPassword(null);
        return hrDto;
    }

    public RespBean updateHrByid(HrDto hr) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = hr.getPassword();
        if (!StringUtils.isEmpty(password)) {
            String encode = bCryptPasswordEncoder.encode(password);
            hr.setPassword(encode);
        }
        int i = hrMapper.updateByPrimaryKeySelectiveDto(hr);
        // 为了 操作简单，我们把要修改的角色，删除，然后重新添加。
        hrRoleMapper.deleteByHrId(hr.getId());
        List<Integer> roleIds = hr.getRoleIds();
        HrRole hrRole = new HrRole();
        hrRole.setHrid(hr.getId());
        // CollectionUtils.isEmpty() 判断list 为空 我们这里取 非空的时候
        if (!CollectionUtils.isEmpty(roleIds)) {
            for (Integer roleid : roleIds) {
                hrRole.setRid(roleid);
                hrRoleMapper.insertSelective(hrRole);
            }

        }
        if (i == 1) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }

    public RespBean save(HrDto hr) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = hr.getPassword();
        if (!StringUtils.isEmpty(password)) {
            String encode = bCryptPasswordEncoder.encode(password);
            hr.setPassword(encode);
        }
        int f = hrMapper.insertSelective(hr);
        List<Integer> roleIds = hr.getRoleIds();
        HrRole hrRole = new HrRole();
        hrRole.setHrid(hr.getId());
        // CollectionUtils.isEmpty() 判断list 为空 我们这里取 非空的时候
        if (!CollectionUtils.isEmpty(roleIds)) {
            for (Integer roleid : roleIds) {
                hrRole.setRid(roleid);
                hrRoleMapper.insertSelective(hrRole);
            }

        }
        if (f == 1) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
}
